package com.leoandroid.tool.toolsbox.largeread.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.leoandroid.tool.toolsbox.largeread.R
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityMyBinding
import com.leoandroid.tool.toolsbox.largeread.utils.StatusBarUtil
import com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils

class MyActivity : AppCompatActivity() {

    var biddingF: ActivityMyBinding? = null
    val binding get() = biddingF!!
    var mFragments: ArrayList<Fragment> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        biddingF = ActivityMyBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.includeTitle.titleTv.text = "设置"
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        if (ToolUtils.getHeightStatusBar(this) > 0) {
            findViewById<View>(R.id.include_title).setPadding(
                0,
                ToolUtils.getHeightStatusBar(this),
                0,
                0
            )
        }
        ToolUtils.setStatusBarHomeTransparent(this)
        StatusBarUtil.setLightMode(this)
        showFragment(com.leoandroid.tool.toolsbox.largeread.fragment.MyFragment.getInstance())
    }

    private fun showFragment(fragment: Fragment) {
        hiddenFragments()
        val fragmentTransaction = supportFragmentManager.beginTransaction();
        if (fragment != null && mFragments.contains(fragment)) {
            fragmentTransaction.show(fragment)
        } else {
            if (fragment != null && !fragment.isAdded) {
                fragmentTransaction.add(R.id.container, fragment)
            }
        }
        fragmentTransaction.commitNowAllowingStateLoss()
    }

    private fun hiddenFragments() {
        if (mFragments.size > 0) {
            for (fragment in mFragments) {
                val fragmentTransient = supportFragmentManager.beginTransaction()
                fragmentTransient.hide(fragment)
                fragmentTransient.commitNowAllowingStateLoss()
            }
        }
    }
}